<template>
  <div>
    <el-dialog title="添加商品规格" :visible.sync="popup.flag">
      <el-form :model="form">
        <el-form-item label="属性名称" :label-width="formLabelWidth">
          <el-input v-model="form.specsname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="属性值"
          :label-width="formLabelWidth"
          v-for="(item, index) in dynamicValidateForm.domains"
          :key="index"
        >
          <el-input
            v-model="item.value"
            autocomplete="off"
            style="width: 70%"
          ></el-input
          ><el-button @click="addDomain" type="warning" v-if="index == 0"
            >新增域名</el-button
          >
          <el-button v-else @click.prevent="removeDomain(index)"
            >删除</el-button
          >
        </el-form-item>
        <el-form-item label="属性状态" :label-width="formLabelWidth">
          <el-switch
            v-model="form.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="popup.flag = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: ["popup"],
  data() {
    return {
      form: {
        // 表单数据
        specsname: "",
        attrs: "",
        status: 1,
      },
      dynamicValidateForm: {
        // 属性值
        domains: [
          {
            value: "",
          },
        ],
      },
      formLabelWidth: "120px",
    };
  },
  computed: {},
  created() {},
  mounted() {},
  watch: {
    popup:{  // 监视弹出框状态
      deep: true,
      handler: function (newval) {
        if (!newval.flag) {
          this.form = {
            specsname: "",
            attrs: "",
            status: 1,
          };
          this.dynamicValidateForm.domains=[{value:''}]
        }
      },
    },
  },
  methods: { 
    addDomain() {  // 新增属性值
     
      this.dynamicValidateForm.domains.push({
        value: "",
        key: Date.now(),
      });
    },
    removeDomain(index) { // 删除属性值
      
      this.dynamicValidateForm.domains.splice(index, 1);
    },
    add() { // 确认事件
      this.form.attrs = this.dynamicValidateForm.domains
        .map((item) => item.value)
        .join(",");


      ( this.form.id? this.$api.postspecsedit(this.form):this.$api.postspecsadd(this.form))
      .then((res) => {
        this.$message({
          message: "恭喜操作成功",
          type: "success",
        });
        this.$emit("getspecslist");
      });
      this.popup.flag = false
    },
  },
  components: {},
};
</script>

<style scoped lang="scss">
::v-deep .iaddregion {
  width: 75%;
}
</style>
